<template>
<section>
  <el-row>
    <el-col :span="6">
      <el-input placeholder="请输入内容" v-model="searchKey" prefix-icon="el-icon-fa-search">
        <div slot="append">
          <el-button type="primary" icon="el-icon-fa-search" @click=" pager.pager.pageNumber = 1 ;doSearch()"></el-button>
        </div>
      </el-input>
    </el-col>
    <el-col :span="6" :offset="12">
      <el-button type="primary" icon="el-icon-fa-plus" @click="addproduct" size="small">添加产品信息</el-button>
    </el-col>
  </el-row>
  <el-table :data="pager.dataList" border stripe style="width: 100%">
    <el-table-column prop="id" label="ID" header-align="center" align="center" width="55">
    </el-table-column>
    <el-table-column prop="name" label="名称" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="productiveTime" label="生产时间" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column prop="manufacturer" label="生产商" show-overflow-tooltip header-align="center" align="center">
    </el-table-column>
    <el-table-column label="操作" show-overflow-tooltip header-align="center" align="center">
      <template slot-scope="scope">
        <el-button-group>
          <el-button title="编辑产品信息" size="mini" type="primary" icon="el-icon-fa-edit" @click="handleEdit(scope.$index,scope.row)"></el-button>
          <el-button title="删除" size="mini" type="primary" icon="el-icon-fa-trash" @click="handleDelete(scope.$index,scope.row)"></el-button>
          <el-button title="查看产品二维码" size="mini" type="primary" icon="el-icon-fa-qrcode" @click="handleQR(scope.$index,scope.row)"></el-button>
        </el-button-group>
      </template>
    </el-table-column>
  </el-table>
  <!--分页条 -->
  <el-row>
    <el-col :span="6" :offset="18">
      <el-pagination background small style="float:right" layout="prev, pager, next" :total="pager.pager.recordCount" :page-size="pager.pager.pageSize" :current-page.sync="pager.pager.pageNumber" v-show="pager.pager.pageCount != 0" @current-change="changePage">
      </el-pagination>
    </el-col>
  </el-row>

  <el-dialog
    title="产品二维码"
    :visible.sync=" QRShow"
    width="30%"
    style="text-align: center;"
    >
    <img  :src="qrUrl" style="margin: 0 auto;" />
  </el-dialog>
  <!-- 弹框区域-->
  <el-dialog :title="product.id == 0 ? '添加产品信息' : '编辑产品信息' " :visible.sync="addEditShow" width="50%">
    <el-form :model="product" :rules="$rules.product" ref="productForm">
      <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="product.name" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写产品名称"></el-input>
      </el-form-item>
      <el-form-item label="原料" :label-width="formLabelWidth" prop="materialIds">
        <el-select multiple v-model="product.materialIds" placeholder="请选择原料">
          <el-option v-for="item in materials" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="生产时间" :label-width="formLabelWidth" prop="productiveTime">
        <el-date-picker
          v-model="product.productiveTime"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择产品生产日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="生产地点" :label-width="formLabelWidth" prop="productiveAddress">
        <el-input v-model="product.productiveAddress" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写产品生产地点"></el-input>
      </el-form-item>
      <el-form-item label="储存方法" :label-width="formLabelWidth" prop="storageMethod">
        <el-input v-model="product.storageMethod" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写产品储存方法"></el-input>
      </el-form-item>
      <el-form-item label="保质期" :label-width="formLabelWidth" prop="qgp">
        <el-input v-model="product.qgp" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写产品保质期"></el-input>
      </el-form-item>
      <div class="order">
        <span style="white-space:pre;">  </span><span class="line"></span>
        <span style="white-space:pre;">  </span><span class="txt">生产商信息</span>
        <span style="white-space:pre;">  </span><span class="line"></span>
      </div>
      <el-form-item label="厂商名称" :label-width="formLabelWidth" prop="manufacturer">
        <el-input v-model="product.manufacturer" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写生产厂商名称"></el-input>
      </el-form-item>
      <el-form-item label="生产商地址" :label-width="formLabelWidth" prop="manufacturer">
        <el-input v-model="product.manufacturerAddress" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写生产商地址"></el-input>
      </el-form-item>
      <el-form-item label="产品执行标准号" :label-width="formLabelWidth" prop="pesNo">
        <el-input v-model="product.pesNo" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写产品执行标准号"></el-input>
      </el-form-item>
      <el-form-item label="食品生产许可证号" :label-width="formLabelWidth" prop="fplNo">
        <el-input v-model="product.fplNo" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写食品生产许可证号"></el-input>
      </el-form-item>
      <el-form-item label="厂商热线" :label-width="formLabelWidth" prop="hotLine">
        <el-input v-model="product.hotLine" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写生产厂商热线"></el-input>
      </el-form-item>
      <el-form-item label="厂商简介" :label-width="formLabelWidth" prop="manufacturerMessage">
        <el-input type="textarea" :rows="3" v-model="product.manufacturerMessage" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写生产厂商简介"></el-input>
      </el-form-item>
      <div class="order">
        <span style="white-space:pre;">  </span><span class="line"></span>
        <span style="white-space:pre;">  </span><span class="txt">质量信息</span>
        <span style="white-space:pre;">  </span><span class="line"></span>
      </div>
      <el-form-item label="厂检编号" :label-width="formLabelWidth" prop="factoryInspectionNo">
        <el-input v-model="product.factoryInspectionNo" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写厂检编号"></el-input>
      </el-form-item>
      <el-form-item label="厂检结果" :label-width="formLabelWidth" prop="factoryInspectionResult">
        <el-input v-model="product.factoryInspectionResult" auto-complete="off" suffix-icon="el-icon-fa-vcard" placeholder="请填写厂检结果"></el-input>
      </el-form-item>
      <el-form-item label="厂检结果证书" :label-width="formLabelWidth" prop="factoryInspectionResultImgKey">
        <el-upload class="avatar-uploader" :action="imgupload" name="imgFile" :data="{key:'factoryInspectionResultImgKey'}" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
          <img v-if="product.factoryInspectionResultImgKey" :src="product.factoryInspectionResultImgKey" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="addEditShow = false ; product = {id:0}">取 消</el-button>
      <el-button type="primary" @click="saveOrUpdateproduct('productForm')">确 定</el-button>
    </div>
  </el-dialog>
</section>
</template>

<style>
.order {
  height: 60px;
  line-height: 60px;
  text-align: center;
}

.order .line {
  display: inline-block;
  width: 150px;
  border-top: 1px solid #ccc;
}

.order .txt {
  color: #686868;
  vertical-align: middle;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>

<script>
import config from "../../common/config";
export default {
  data() {
    return {
      imgupload: config.http.baseUrl + "image/kind/upload",
      searchKey: "",
      pager: {
        pager: {
          pageCount: 1,
          pageNumber: 1,
          pageSize: 15,
          recordCount: 1
        },
        dataList: []
      },
      types: [],
      materials: [],
      addEditShow: false,
      QRShow: false,
      imageUrl: "",
      formLabelWidth: "140px",
      product: {
        id: 0
      },
      manors: [],
      products: [],
      transports: []
    };
  },
  computed: {
    qrUrl: function() {
      return config.http.baseUrl + "image/qr/" + this.product.id;
    }
  },
  methods: {
    loadData() {
      this.$api.Product.list(this.pager.pager.pageNumber, result => {
        this.pager = result.pager;
      });
    },
    addproduct() {
      this.addEditShow = true;
      this.product = {
        id: 0
      };
    },
    handleEdit(index, row) {
      this.product = JSON.parse(JSON.stringify(row));
      // this.product.type = this.product.type.code;
      this.addEditShow = true;
    },
    handleDelete(index, row) {
      this.$confirm("确认删除该产品信息?", "删除确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$api.Product.delete(row.id, result => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          window.setTimeout(() => {
            if (this.searchKey) {
              this.doSearch();
            } else {
              this.loadData();
            }
          }, 2000);
        });
      });
    },
    handleQR(index, row) {
      this.product = row;
      this.QRShow = true;
    },
    changePage() {
      if (this.searchKey) {
        this.doSearch();
      } else {
        this.loadData();
      }
    },
    doSearch() {
      this.$api.Product.search(
        this.pager.pager.pageNumber,
        this.searchKey,
        result => {
          this.pager = result.pager;
        }
      );
    },
    saveOrUpdateproduct(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var callback = result => {
            this.changePage();
            this.addEditShow = false;
          };
          this.product.id
            ? this.$api.Product.update(this.product, callback)
            : this.$api.Product.save(this.product, callback);
        } else {
          return false;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.product[res.key] = config.http.baseUrl + res.url;
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isIMG = file.type.substr(0, "image/".length) === "image/";
      console.log(isIMG);
      const isLt5M = file.size / 1024 / 1024 < 5;

      if (!isIMG) {
        this.$message.error("只能上传图片格式!");
      }
      if (!isLt5M) {
        this.$message.error("上传图片大小不能超过 5 MB!");
      }
      return isIMG && isLt5M;
    }
  },
  created() {
    this.$api.Product.loadInfos(result => {
      this.types = result.types;
      this.materials = result.materials;
      this.loadData();
    });
  }
};
</script>
